/*
 * @文件描述: 时间，电压参数复选框
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-01-28 16:24:01
 */
#include "measureWindowUI.h"
#ifdef GUI_UCGUI

//EventsFunctionList
void OnCheckBox4ValueChanged(WM_MESSAGE *pMsg);
void OnNextClicked(WM_MESSAGE *pMsg);
void OnPreviousClicked(WM_MESSAGE *pMsg);
void OnCheckBox3ValueChanged(WM_MESSAGE *pMsg);
void OnCheckBox2ValueChanged(WM_MESSAGE *pMsg);
void OnCheckBox1ValueChanged(WM_MESSAGE *pMsg);
void OnCheckBox0ValueChanged(WM_MESSAGE *pMsg);
//EndofEventsFunctionList

/*********************************************************************
*
*       static data
*
**********************************************************************
*/
static WM_HWIN HWin;
static int currentPage = 0;
static unsigned char ListSize;

static OscillInfo *dataList;
/*********************************************************************
*
*       Dialog resource
*
* This table conatins the info required to create the dialog.
* It has been created by ucGUIbuilder.
*/

static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
    {FRAMEWIN_CreateIndirect, "Measure", 0, 0, 0, 255, 420, FRAMEWIN_CF_MOVEABLE, 0},
    {CHECKBOX_CreateIndirect, "CHECKBOX0", GUI_ID_CHECK0, 15, 16, 214, 45, 0, 0},
    {CHECKBOX_CreateIndirect, "CHECKBOX1", GUI_ID_CHECK1, 15, 67, 214, 45, 0, 0},
    {CHECKBOX_CreateIndirect, "CHECKBOX2", GUI_ID_CHECK2, 15, 118, 214, 45, 0, 0},
    {CHECKBOX_CreateIndirect, "CHECKBOX3", GUI_ID_CHECK3, 15, 169, 214, 45, 0, 0},
    {BUTTON_CreateIndirect, "Previous", GUI_ID_BUTTON0, 15, 292, 84, 54, 0, 0},
    {BUTTON_CreateIndirect, "Next", GUI_ID_BUTTON1, 145, 292, 84, 54, 0, 0},
    {CHECKBOX_CreateIndirect, "CHECKBOX4", GUI_ID_CHECK4, 15, 220, 214, 45, 0, 0}};

/*****************************************************************
**      FunctionName:void PaintDialog(WM_MESSAGE * pMsg)
**      Function: to initialize the Dialog items
**                                                      
**      call this function in _cbCallback --> WM_PAINT
*****************************************************************/

static void PaintDialog(WM_MESSAGE *pMsg)
{
    WM_HWIN hWin = pMsg->hWin;
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK0), dataList[0 + currentPage * 5].isShowed);
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK1), dataList[1 + currentPage * 5].isShowed);
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK2), dataList[2 + currentPage * 5].isShowed);
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK3), dataList[3 + currentPage * 5].isShowed);
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK4), dataList[4 + currentPage * 5].isShowed);

    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK0), dataList[0 + currentPage * 5].str);
    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK1), dataList[1 + currentPage * 5].str);
    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK2), dataList[2 + currentPage * 5].str);
    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK3), dataList[3 + currentPage * 5].str);
    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK4), dataList[4 + currentPage * 5].str);
    printf("rePaint window currentpage:%d\n", currentPage);
}

/*****************************************************************
**      FunctionName:void InitDialog(WM_MESSAGE * pMsg)
**      Function: to initialize the Dialog items
**                                                      
**      call this function in _cbCallback --> WM_INIT_DIALOG
*****************************************************************/

static void InitDialog(WM_MESSAGE *pMsg)
{
    WM_HWIN hWin = pMsg->hWin;
    //
    //FRAMEWIN
    //
    FRAMEWIN_SetFont(hWin, &GUI_Font32_ASCII);
    FRAMEWIN_SetTextAlign(hWin, GUI_TA_VCENTER | GUI_TA_LEFT);
    FRAMEWIN_AddCloseButton(hWin, FRAMEWIN_BUTTON_RIGHT, 0);
    FRAMEWIN_SetTitleHeight(hWin, 57);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON0), &GUI_Font24_ASCII);
    BUTTON_SetFont(WM_GetDialogItem(hWin, GUI_ID_BUTTON1), &GUI_Font24_ASCII);
    //
    //GUI_ID_CHECK0
    //
    CHECKBOX_SetFont(WM_GetDialogItem(hWin, GUI_ID_CHECK0), &GUI_Font24_ASCII);

    CHECKBOX_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_CHECK0), GUI_TA_VCENTER | GUI_TA_CENTER);
    //
    //GUI_ID_CHECK1
    //
    CHECKBOX_SetFont(WM_GetDialogItem(hWin, GUI_ID_CHECK1), &GUI_Font24_ASCII);

    CHECKBOX_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_CHECK1), GUI_TA_VCENTER | GUI_TA_CENTER);
    //
    //GUI_ID_CHECK2
    //
    CHECKBOX_SetFont(WM_GetDialogItem(hWin, GUI_ID_CHECK2), &GUI_Font24_ASCII);

    CHECKBOX_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_CHECK2), GUI_TA_VCENTER | GUI_TA_CENTER);
    //
    //GUI_ID_CHECK3
    //
    CHECKBOX_SetFont(WM_GetDialogItem(hWin, GUI_ID_CHECK3), &GUI_Font24_ASCII);

    CHECKBOX_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_CHECK3), GUI_TA_VCENTER | GUI_TA_CENTER);
    //
    //GUI_ID_CHECK4
    //
    CHECKBOX_SetFont(WM_GetDialogItem(hWin, GUI_ID_CHECK4), &GUI_Font24_ASCII);

    CHECKBOX_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_CHECK4), GUI_TA_VCENTER | GUI_TA_CENTER);

    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK0), dataList[0 + currentPage * 5].isShowed);
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK1), dataList[1 + currentPage * 5].isShowed);
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK2), dataList[2 + currentPage * 5].isShowed);
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK3), dataList[3 + currentPage * 5].isShowed);
    CHECKBOX_SetState(WM_GetDialogItem(hWin, GUI_ID_CHECK4), dataList[4 + currentPage * 5].isShowed);

    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK0), dataList[0 + currentPage * 5].str);
    printf("item 0:%s\n", dataList[0 + currentPage * 5].str);
    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK1), dataList[1 + currentPage * 5].str);
    printf("item 1:%s\n", dataList[1 + currentPage * 5].str);
    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK2), dataList[2 + currentPage * 5].str);
    printf("item 2:%s\n", dataList[2 + currentPage * 5].str);
    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK3), dataList[3 + currentPage * 5].str);
    printf("item 3:%s\n", dataList[3 + currentPage * 5].str);
    CHECKBOX_SetText(WM_GetDialogItem(hWin, GUI_ID_CHECK4), dataList[4 + currentPage * 5].str);
    printf("item 4:%s\n", dataList[4 + currentPage * 5].str);
}

/*********************************************************************
*
*       Dialog callback routine
*/
static void _cbCallback(WM_MESSAGE *pMsg)
{
    int NCode, Id;
    WM_HWIN hWin = pMsg->hWin;

    switch (pMsg->MsgId)
    {
    case WM_DELETE:
        HWin = NULL;
        //printf("measureWindow closed\n");

        break;

    case WM_PAINT:
        PaintDialog(pMsg);

        break;
    case WM_INIT_DIALOG:
        InitDialog(pMsg);
        break;
    case WM_KEY:
        switch (((WM_KEY_INFO *)(pMsg->Data.p))->Key)
        {
        case GUI_KEY_ESCAPE:
            GUI_EndDialog(hWin, 1);
            break;
        case GUI_KEY_ENTER:
            GUI_EndDialog(hWin, 0);
            break;
        }
        break;
    case WM_NOTIFY_PARENT:
        Id = WM_GetId(pMsg->hWinSrc);
        NCode = pMsg->Data.v;
        switch (Id)
        {
        case GUI_ID_OK:
            if (NCode == WM_NOTIFICATION_RELEASED)
                GUI_EndDialog(hWin, 0);
            break;
        case GUI_ID_CANCEL:
            if (NCode == WM_NOTIFICATION_RELEASED)
                GUI_EndDialog(hWin, 0);
            break;
        case GUI_ID_CHECK0:
            switch (NCode)
            {
            case WM_NOTIFICATION_VALUE_CHANGED:
                OnCheckBox0ValueChanged(pMsg);
                break;
            }
            break;
        case GUI_ID_CHECK1:
            switch (NCode)
            {
            case WM_NOTIFICATION_VALUE_CHANGED:
                OnCheckBox1ValueChanged(pMsg);
                break;
            }
            break;
        case GUI_ID_CHECK2:
            switch (NCode)
            {
            case WM_NOTIFICATION_VALUE_CHANGED:
                OnCheckBox2ValueChanged(pMsg);
                break;
            }
            break;
        case GUI_ID_CHECK3:
            switch (NCode)
            {
            case WM_NOTIFICATION_VALUE_CHANGED:
                OnCheckBox3ValueChanged(pMsg);
                break;
            }
            break;
        case GUI_ID_BUTTON0:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnPreviousClicked(pMsg);
                break;
            }
            break;
        case GUI_ID_BUTTON1:
            switch (NCode)
            {
            case WM_NOTIFICATION_CLICKED:
                OnNextClicked(pMsg);
                break;
            }
            break;
        case GUI_ID_CHECK4:
            switch (NCode)
            {
            case WM_NOTIFICATION_VALUE_CHANGED:
                OnCheckBox4ValueChanged(pMsg);
                break;
            }
            break;
        }
        break;
    default:
        WM_DefaultProc(pMsg);
    }
}

/*********************************************************************
*
*       MainTask
*
**********************************************************************
*/

void createMeasureWin(GUI_HWIN hParent)
{
    if (HWin == NULL)
    {
        WM_SetCreateFlags(WM_CF_MEMDEV);
        HWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbCallback, hParent, 505, 30);
        //WM_HideWindow(HWin);
    }
}
void showMeasureWin(void)
{
    WM_ShowWindow(HWin);
    //GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbCallback, hParent, 505, 30);
}
void closeMeasureWin(void)
{
    WM_HideWindow(HWin);
}
/**
 * @brief : 获取当前的页数
 * @param {*}
 * @return {*}
 */
int getCurrentPage(void)
{
    return currentPage;
}
/**
 * @brief : 设置当前的页数，改函数将改变列表中的数据
 * @param {int} pageNum
 * @return {*}
 */
void setCurrentPage(int pageNum, WM_MESSAGE *pMsg)
{
    currentPage = pageNum;
    WM_InvalidateWindow(WM_GetClientWindow(pMsg->hWin));
    //WM_Paint(WM_GetClientWindow(pMsg->hWin));
}

/**设置显示参数结构体
 * @brief : 
 * @param {*}
 * @return {*}
 */
void onViewSetMeasureDataList(OscillInfo *list)
{
    dataList = list;
}
void setItemState(uint8_t rank, uint8_t state)
{
    dataList[rank + currentPage * 5].isShowed = state;
}
#endif